package com.puppy.mapper;

import com.puppy.pojo.Emp;
import com.puppy.pojo.EmpQueryParam;
import com.puppy.pojo.LoginInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Transactional(rollbackFor = {Exception.class})//任何异常都回滚
@Mapper
public interface EmpMapper {

    /// /    原始页面查询
//    @Select("select count(*) from emp e left join depts d on d.id = e.dept_id")
//    Long count();
//
//    //分页查询；查询页码，每页显示条数
//    @Select("select e.*,d.name deptName from emp e left join depts d on e.dept_id = d.id order by e.update_time desc " +
//            "limit #{start},#{pageSize};")
//    List<Emp> list(Integer start, Integer pageSize);

//使用分页查询插件PageHelper
//    @Select("select e.*,d.name deptName from emp e left join depts d on e.dept_id = d.id order by e.update_time desc ")
//    List<Emp> list();

//    @Select("select e.*,d.name deptName from emp e left join depts d on e.dept_id = d.id " +
//            "where e.name like concat('%',#{name},'%') and e.gender=#{gender} and e.entry_date between #{begin} and #{end}" +
//            " order by update_time desc")
//     List<Emp> list(String name, Integer gender, LocalDate begin, LocalDate end);

    List<Emp> list(EmpQueryParam empQueryParam);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    //主键返回，用emp中的变量id获取生成的主键值-只有存进去我们才知道主键的值
    @Insert("insert into emp(username, name, gender, phone, job, salary, image, entry_date, dept_id, create_time, update_time) " +
            "values (#{username},#{name},#{gender},#{phone},#{job},#{salary},#{image},#{entryDate},#{deptId},#{createTime},#{updateTime}) ")
    void insert(Emp emp);

    void deleteByIds(List<Integer> ids);

    Emp getById(Integer id);

    void update(Emp emp);

    /*统计员工职位人数*/
    @MapKey("job")  //job是sql语句中查询结果的键值，自己在sql查询中取得别名
    //是Mybatis报错的解决方法，没有用到，但是必须写，否则报错，除非setting中关闭错误提示
    List<Map<String, Object>> countEmpJobData();

    /*统计员工性别人数*/
    @MapKey("name") //同上
    List<Map<String, Object>> countEmpGenderData();

    @Select("select emp.id,emp.username,emp.name from emp where username = #{username} and password = #{password}")
    LoginInfo logByNameAndPassword(Emp emp); //自动封装结果到LoginInfo对象中，token设置为null
}
